package simtester.agent

import static org.junit.Assert.*

import java.util.logging.FileHandler
import java.util.logging.Logger
import org.junit.Test
import org.junit.After


class AgentLogFormatterTests {
    
    @After
    void tearDown() {
        new File('.').eachFile { file ->
            if (file.isFile() && file.name =~ /^agent\.log\.?\d*$/) {  //$
                println file.text
                file.delete()
            }
        }
    }

    @Test
    void createLogEntry() {
        def logger = Logger.getLogger('agentlogtest')
        def fh = new FileHandler('agent.log')
        fh.setFormatter(new AgentLogFormatter())
        logger.addHandler(fh)
        
        logger.info('a test info message')
        logger.severe('log an error')
    
		
		try {    
			def text = new File('agent.log').text
			assert text =~ /\d\d\/\d\d\/\d{4} \d\d:\d\d:\d\d\.\d+ INFO: a test info message/
			assert text =~ /\d\d\/\d\d\/\d{4} \d\d:\d\d:\d\d\.\d+ SEVERE: log an error/
		} finally {
			logger.handlers.each { h ->
				h.close()
			}
		}
	}
}
